package com.hll.mybatis_plus.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.env.Profiles;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;

/**
 * @author Mr.Hu
 * @since 2021-03-18 12:07
 */
@Configuration
@EnableSwagger2
public class SwaggerConfig {
  /*swagger2 ui配置*/
  @Bean
  public Docket createRestApi(Environment environment) {
    // 设置要显示swagger的环境，参数可多选 ("dev","test"...)
    Profiles of = Profiles.of("dev"); // 如果spring.profiles.active=test则为false，注释掉也是false
    boolean enable = environment.acceptsProfiles(of);
    return new Docket(DocumentationType.SWAGGER_2)
        .apiInfo(apiInfo())
        .enable(enable) // 配置是否启用Swagger，如果是false，在浏览器将无法访问
        .select()
        .apis(RequestHandlerSelectors.basePackage("com.hll.mybatis_plus"))
        .paths(PathSelectors.any())
        .build();
  }

  private ApiInfo apiInfo() {
    Contact contact = new Contact("hll", "https://www.hll.com", "2621365348@qq.com");
    return new ApiInfo(
        "Swagger学习", // 标题
        "学习演示如何配置Swagger", // 描述
        "v1.0", // 版本
        "https://www.hll.com", // 组织链接
        contact, // 联系人信息
        "Apache 2.0 许可", // 许可
        "许可链接", // 许可连接
        new ArrayList<>() // 扩展
        );
  }
}
